package com.snut.tomorrowNews.controller.back;

import com.snut.tomorrowNews.common.CommonResult;
import com.snut.tomorrowNews.model.Admin;
import com.snut.tomorrowNews.model.News;
import com.snut.tomorrowNews.model.NewsType;
import com.snut.tomorrowNews.service.back.NewsService;
import com.snut.tomorrowNews.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
public class newsController {

    @Autowired
    NewsService newsService;

    @RequestMapping("getNewsList")
    public CommonResult<List<News>> getNewsList() {
        CommonResult<List<News>> commonResult = null;
        try {
            List<News> newsList = newsService.getNewsList();
            commonResult = new CommonResult<>(200, "查询成功", newsList);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    @RequestMapping("getNewsTypeList")
    public CommonResult<List<NewsType>> getNewsTypeList() {
        CommonResult<List<NewsType>> commonResult = null;
        try {
            List<NewsType> newsTypeList = newsService.getNewsTypeList();
            commonResult = new CommonResult<>(200, "查询成功", newsTypeList);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    @RequestMapping("addNews")
    public CommonResult addNews(@RequestParam("coverPicture") CommonsMultipartFile file, String title, Integer newsId, String summary, String content, HttpSession session) {
        CommonResult commonResult = null;
        Admin admin = (Admin) session.getAttribute("admin");
        Integer adminId = admin.getId();
        File f = new File("D:\\test\\tomorrowNewsFiles\\news");
        if (!f.exists()) {
            f.mkdir();
        }
        //防止文件重名,生成保存其新文件名
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.getNewFileName(oldFileName);
        News news = new News();
        news.setTitle(title);
        news.setNewsId(newsId);
        news.setSummary(summary);
        news.setContent(content);
        news.setOperater(adminId);
        news.setCoverPicture(newFileName);
        news.setState(0);
        newsService.addNews(news);
        File f1 = new File(f, newFileName);
        try {
            //将文件输出到指定的目录
            file.transferTo(f1);
            //返回文件名
            commonResult = new CommonResult<>(200, "保存成功", null);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult = new CommonResult<>(500, "服务器忙", null);
        }
        return commonResult;
    }

    @RequestMapping("addNewsAndPush")
    public CommonResult addNewsAndPush(@RequestParam("coverPicture") CommonsMultipartFile file, String title, Integer newsId, String summary, String content, HttpSession session) {
        CommonResult commonResult = null;
        Admin admin = (Admin) session.getAttribute("admin");
        Integer adminId = admin.getId();
        File f = new File("D:\\test\\tomorrowNewsFiles\\news");
        if (!f.exists()) {
            f.mkdir();
        }
        //防止文件重名,生成保存其新文件名
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.getNewFileName(oldFileName);
        News news = new News();
        news.setTitle(title);
        news.setNewsId(newsId);
        news.setSummary(summary);
        news.setContent(content);
        news.setOperater(adminId);
        news.setCoverPicture(newFileName);
        news.setState(2);
        newsService.addNews(news);
        File f1 = new File(f, newFileName);
        try {
            //将文件输出到指定的目录
            file.transferTo(f1);
            //返回文件名
            commonResult = new CommonResult<>(200, "保存成功", null);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult = new CommonResult<>(500, "服务器忙", null);
        }
        return commonResult;
    }

    @RequestMapping("deleteNews")
    public CommonResult deleteNews(Integer id){
        CommonResult commonResult = null;
        try{
            newsService.deleteNews(id);
            commonResult = new CommonResult(200,"删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙",null);
        }
        return commonResult;
    }

    @RequestMapping("statisticsByType")
    public CommonResult statisticsByType(){
        CommonResult commonResult = null;
        try{
            Map<String,List> map = newsService.statisticsByType();
            commonResult = new CommonResult(200,"删除成功",map);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙",null);
        }
        return commonResult;
    }


    @RequestMapping("getNewsById")
    public CommonResult<News> getNewsById(Integer newsId) {
        CommonResult<News> commonResult = null;
        try {
            News news = newsService.getNewsById(newsId);
            commonResult = new CommonResult<>(200, "查询成功", news);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }



}
